import Vue from 'vue'
import App from './App.vue'
import router from './routers/router'
import store from './store/store'
import { mapMutations } from 'vuex'
import ElementUI from 'element-ui';
import CKEditor from '@ckeditor/ckeditor5-vue';

import 'element-ui/lib/theme-chalk/index.css';
import './assets/cadmin.css'
import 'element-ui/lib/theme-chalk/base.css';

//使用axios
import axios from 'axios'
import VueAxios from 'vue-axios'

import { get, post, postform } from './utils/http'
Vue.prototype.$get = get;
Vue.prototype.$post = post;
Vue.prototype.$postform = postform;

//加载字体图标
import './assets/fonts/fontawesome-free-5.9.0/css/fontawesome.css'
import './assets/fonts/fontawesome-free-5.9.0/css/solid.css'
//加载公用样式
import './assets/css/style.css'
process.env.NODE_ENV === 'development' && require('@/mock')

axios.interceptors.request.use(
  (config) => {
    // 所有请求之前都要执行的操作

    //根据接口传入参数显示加载动画
    showFullScreenLoading();

    return config;
  }, function (err) {
    return Promise.reject(err);
  }
);
//http response 拦截器
axios.interceptors.response.use(response => {
  // 所有请求完成后都要执行的操作
  //暂停加载动画
  tryHideFullScreenLoading();
  return response;
}, err => {
  tryHideFullScreenLoading();
  if (err.response) {
    switch (err.response.status) {
      case 401:
        // 返回 401 清除token信息并跳转到登录页面
        store.dispatch("logout");
        router.replace({
          path: 'login',
          query: { redirect: router.history.current.fullPath }
        });
        break;
      case 500:
          // router.replace({
          //   path: 'login',
          //   query: { redirect: router.history.current.fullPath }
          // })
      break;
    }
  }
  return Promise.reject(err);
});


let needLoadingRequestCount = 0;
function showFullScreenLoading() {
  if (needLoadingRequestCount === 0) {
    startLoading();
  }
  needLoadingRequestCount++;
}
function tryHideFullScreenLoading() {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    endLoading();
  }
}
let loading;
function startLoading() {
  loading = Vue.prototype.$loading({
    lock: true,
    text: "加载中...",
    background: "rgba(0, 0, 0, 0.8)"
  });
}

function endLoading() {
  loading.close();
}

Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(CKEditor);
Vue.use(VueAxios, axios)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
